package cn.tedu.movie.controller;

import cn.tedu.movie.entity.Actor;
import cn.tedu.movie.entity.Movie;
import cn.tedu.movie.entity.MovieVo;
import cn.tedu.movie.mapper.ActorMapper;
import cn.tedu.movie.mapper.MovMapper;
import cn.tedu.movie.mapper.MovieMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/actor/")
public class ActorController {
    private static final String DIRPath = "C:/upload/movie";
    @Autowired(required = false)
    ActorMapper actorMapper;
    @Autowired(required = false)
    MovieMapper movieMapper;

    @GetMapping("searchByName")
    List<Actor> searchByName(String name) {
        List<Actor> actorList = actorMapper.searchByName(name);
        if (actorList.isEmpty()) {
            actorList = actorMapper.searchActorByMovieName(name);
        }
        for (int i = 0; i < actorList.size(); i++) {
            Actor actor = actorList.get(i);
            List<String> nameList = movieMapper.getRep(actor.getActorId());
            StringBuilder sb = new StringBuilder();
            for (String s : nameList) {
                sb.append("《");
                sb.append(s);
                sb.append("》");
            }
            actor.setRep(sb.toString());
            actorList.set(i, actor);
        }
        return actorList;
    }

    //-----------------------------------------后端-----------------------------------
    @GetMapping("actorlist")
    List<Actor> movieList(Integer page, Integer limit, String name) {
        if (name == null)
            name = "";
        name = name.trim();
        List<Actor> list = actorMapper.actorlist((page - 1) * limit, limit, name);
        if (list.size() > 0)
            list.get(0).setCount(actorMapper.actorCount(name));
        return list;
    }

    @PostMapping("insertActor")
    int insertActor(Actor actor, MultipartFile picFile) throws IOException {
        String fileName = picFile.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID() + suffix;
        //得到和日期相关的路径
        SimpleDateFormat format =
                new SimpleDateFormat("/yyyy/MM/dd/");
        //把当前时间按照指定的格式进行转换 /2021/10/19/
        String datePath = format.format(new Date());
        //得到保存图片文件夹的路径f:/upload/vrd/2021/10/19/
        File dirFile = new File(DIRPath + datePath);
        //如果不存在则创建
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        //得到完成的文件路径
        String filePath = DIRPath + datePath + fileName;
        //保存上传的文件
        picFile.transferTo(new File(filePath));
        actor.setActorUrl(datePath + fileName);
        int num = actorMapper.insertActor(actor);
        return num;
    }

    @GetMapping("deleteById")
    public int deleteById(Integer id) {
        String url = actorMapper.selectUrlById(id);
        String filePath = DIRPath + url;
        new File(filePath).delete();
        int num = actorMapper.deleteById(id);
        return num;
    }
    @PostMapping("deleteByIds")
    public void deleteByIds(String ids) {
        String[] strs = ids.split("[\\[\\],]");
        for (String str : strs) {
            if (!str.isEmpty()){
                Integer id=Integer.parseInt(str);
                String url = actorMapper.selectUrlById(id);
                String filePath = DIRPath + url;
                new File(filePath).delete();
                int num = actorMapper.deleteById(id);
            }
        }
    }
    @GetMapping("backstageSelectOne")
    public Actor backstageSelectOne(Integer id){
        return actorMapper.selectOne(id);
    }
    @PostMapping("updateActor")
    public int updateActor(Actor actor){
        return actorMapper.updateActor(actor);
    }
}
